﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class KoliIrsaliyeOlustur : XForm
    {
        public KoliIrsaliyeOlustur()
        {
            InitializeComponent();
        }

        int nextIrsaliye, IrsaliyeId = 0;

        public KoliIrsaliyeOlustur(int _irsaliyeId):this()
        {
            IrsaliyeId = _irsaliyeId;
        }

        int CariId;
        public KoliIrsaliyeOlustur(string cikanPartiIds,int cariId)
            : this()
        {
            CikanPartiIds = cikanPartiIds;
            CariId = cariId;
        }
        
        private void KumasIrsaliyeOlustur_Load(object sender, EventArgs e)
        {            
            gle.Properties.Assign(RI(Rp.Cari));
            gleSevkiyatci.Properties.Assign(RI(Rp.Sevkiyatci));
            DataTable dt = ExecuteDataTable("GetKumasIrsaliyeNo", cmbTip.SelectedIndex);
            DataRow dr = dt.Rows[0];

            txtIrsaliyeNo.Text = String.Format("{0:000000}", intParse(dr["IrsaliyeNumber"]) + 1);
            
            dateIrsaliye.EditValue = DateTime.Now;
            dateSevk.EditValue = DateTime.Now;

            gle.EditValue = CariId;

            btnOk.Click += new EventHandler(btnOk_Click);
            cmbTip.SelectedIndexChanged += new EventHandler(cmbTip_SelectedIndexChanged);
            cbTip.SelectedIndexChanged += new EventHandler(cbTip_SelectedIndexChanged);
            btnCancel.Click += new EventHandler(btnCancel_Click);
        }

        string CikanPartiIds;
        void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        void cbTip_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbTip.Properties.ReadOnly = false;
            gle.Properties.ReadOnly = false; 

            DataTable dt = ExecuteDataTable("GetKumasIrsaliyeNo", cbTip.SelectedIndex);
            DataRow dr = dt.Rows[0];
            txtIrsaliyeNo.Text = String.Format("{0:000000}", intParse(dr["IrsaliyeNumber"]) + 1);
        }

        void cmbTip_SelectedIndexChanged(object sender, EventArgs e)
        {
            gle.EditValue = null;

            switch (cmbTip.SelectedIndex)
            {
                case 0:
                    gle.Properties.Assign(RI(Rp.V_CariA));
                    break;
                case 1:
                    gle.Properties.Assign(RI(Rp.V_CariC));
                    break;
                case 2:
                    gle.Properties.Assign(RI(Rp.V_CariV));
                    break;
            }            
        }

        void btnOk_Click(object sender, EventArgs e)
        {           

            if (intParse(gle.EditValue) == 0)
            {
                ShowMessage("Lütfen cari seçin !");
                return;
            }
            nextIrsaliye = (Convert.ToInt32(txtIrsaliyeNo.Text)) - 1;

            Rapor.KoliIrsaliye.KoliIrsaliye irs;
            string TeslimEden = string.Empty;
            if (gleSevkiyatci.EditValue != null)
                TeslimEden = AppSession.Lookups.Sevkiyatci.FindBySevkiyatciId(intParse(gleSevkiyatci.EditValue)).Name;

            //if (CikanPartiIds != "" && CikanPartiIds != null)
            //{
            //    irs = new Rapor.KoliIrsaliye.KoliIrsaliye(txtIrsaliyeNo.Text, dateSevk.DateTime, txtNot.Text,  WhoAmI, cmbTip.SelectedIndex, intParse(gle.EditValue), CikanPartiIds, TeslimEden);
            //}
            //else

            irs = new Rapor.KoliIrsaliye.KoliIrsaliye(txtIrsaliyeNo.Text, dateSevk.DateTime, txtNot.Text, WhoAmI, cmbTip.SelectedIndex, intParse(gle.EditValue), CikanPartiIds, TeslimEden, IrsaliyeId);

            irs.PrintProgress += new DevExpress.XtraPrinting.PrintProgressEventHandler(irs_PrintProgress);
            irs.PrintingSystem.StartPrint += new DevExpress.XtraPrinting.PrintDocumentEventHandler(PrintingSystem_StartPrint);
            irs.PrintingSystem.EndPrint += PrintingSystem_EndPrint;
            irs.PrinterName = "SAMSUNG ML 2550";            
            irs.ShowPreview();

        }

        void PrintingSystem_EndPrint(object sender, EventArgs e)
        {
            if (CikanPartiIds != "" && CikanPartiIds != null)
                ExecuteNonQuery("UpdPartiSevkiyatci", CikanPartiIds, gleSevkiyatci.EditValue);
        }

        void PrintingSystem_StartPrint(object sender, DevExpress.XtraPrinting.PrintDocumentEventArgs e)
        {            
            e.PrintDocument.PrinterSettings.Copies = 4;
            e.PrintDocument.PrinterSettings.Collate = false;
        }

        void irs_PrintProgress(object sender, DevExpress.XtraPrinting.PrintProgressEventArgs e)
        {
            if (CikanPartiIds != "" && CikanPartiIds != null)
            {
                nextIrsaliye++;
                Session.DataManager.ExecuteDataSet("createKoliIrsaliyeByCikanParti", txtIrsaliyeNo.Text, dateSevk.DateTime, txtNot.Text, nextIrsaliye, cbTip.SelectedIndex, CikanPartiIds);
            }
            else if (IrsaliyeId > 0)
            {
                nextIrsaliye++;
                Session.DataManager.ExecuteDataSet("createKumasIrsaliyeByIrsaliyeId", txtIrsaliyeNo.Text, dateSevk.DateTime, txtNot.Text, nextIrsaliye, cbTip.SelectedIndex, IrsaliyeId, CikanPartiIds, cmbTip.SelectedIndex, intParse(gle.EditValue));
            }

            this.DialogResult = DialogResult.OK;
        }
    }
}
